import { useDispatch, useSelector } from "react-redux"
import { getBillList, addBillList } from "@/store/modules/billStore";
import { useEffect, useMemo } from "react";
import { transDateList } from "@/utils";

export const useBillList = () => {
  const dispatch = useDispatch()
  const billList = useSelector(state => state.bill.billList)

  useEffect(() => {
    dispatch(getBillList())
  }, [dispatch])

  return {
    billList
  }
}

export const useYearBillList = (selectedYear) => {
  const { billList } = useBillList()

  const yearBills = useMemo(() => {
    return transDateList(billList, 'YYYY')[selectedYear]
  }, [billList, selectedYear])

  return yearBills || []
}